package group2assignone.view.customer;

import group2assignone.func.UCCtoALAdapter;
import group2assignone.model.Customer;
import group2assignone.model.Flight;
import group2assignone.ucm.UCLogout;
import group2assignone.ucm.customer.UCBookFlight;
import group2assignone.ucm.customer.UCCancelCustomer;
import group2assignone.ucm.customer.UCCancelGuest;
import group2assignone.view.OurView;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

/**
 *
 * @author group2
 */
public class BookFlightView extends OurView {

    private static final int textSize = 10; //size of text box
    private JLabel custLab;
    private JButton logout = new JButton("Logout");
    private JButton bookFlight = new JButton("Book");
    private JButton cancel = new JButton("Cancel");
    private JTextField noTickets = new JTextField(textSize);
    private JTextField fName = new JTextField(textSize);
    private JTextField lName = new JTextField(textSize);
    private JTextField dob = new JTextField(textSize);
    private JLabel noTicketsLab = new JLabel("How Many Tickets?");
    private JLabel fNameLab = new JLabel("First Name: ");
    private JLabel lNameLab = new JLabel("Last Name: ");
    private JLabel dobLab = new JLabel("Date Of Birth: ");
    private GridLayout grid;
    private Customer customer;
    private Flight flight;

    public BookFlightView(Customer customer, Flight flight) {
        this.customer = customer;
        this.flight = flight;
    }

    /**
     * Function to add the GUI components to the frame.
     */
    @Override
    public void addComponents() {

        setTitle("Booking Details");
        custLab = new JLabel("Welcome " + customer.getUsername());

        custLab = new JLabel("Welcome " + customer.getUsername());
        northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.LINE_AXIS));
        custLab.setAlignmentX(JLabel.LEFT);
        logout.setAlignmentX(JButton.RIGHT);
        northPanel.add(custLab);
        northPanel.add(Box.createHorizontalGlue());
        northPanel.add(Box.createVerticalStrut(10));
        if (!customer.getUsername().equals("Guest")) {
            northPanel.add(logout);
        }

        grid = new GridLayout(4, 2, 2, 1);
        centerPanel.setLayout(grid);
        grid.setVgap(20);

        centerPanel.add(noTicketsLab);
        centerPanel.add(noTickets);
        centerPanel.add(fNameLab);
        centerPanel.add(fName);
        centerPanel.add(lNameLab);
        centerPanel.add(lName);
        centerPanel.add(dobLab);
        centerPanel.add(dob);

        southPanel.add(bookFlight);
        southPanel.add(cancel);

        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        northPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        southPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        logout.addActionListener(new UCCtoALAdapter(new UCLogout(this, customer)));
        //bookFlight.addActionListener(new UCCtoALAdapter(new UCBookFlight(customer, flight, this)));
        if (customer.getUsername().equals("Guest")) {
            cancel.addActionListener(new UCCtoALAdapter(new UCCancelGuest(this)));
        } else {
            cancel.addActionListener(new UCCtoALAdapter(new UCCancelCustomer(customer, this)));
        }
        bookFlight.addActionListener(new UCCtoALAdapter(new UCBookFlight(customer, flight, this)));
    }

    public int getNumberTickets()
    {
        return Integer.parseInt(noTickets.getText());
    }

    /**
     * Displays a confirm option to the user
     *
     * @return int option - returns int depending on JOptionPane.YES_NO_OPTION
     */
    public int confirmBook() {

        return JOptionPane.showConfirmDialog(
                this,
                "Are you sure you want to book this flight?",
                "Confirm",
                JOptionPane.YES_NO_OPTION);
    }

}
